"""Deals with the application preferences"""

from PyQt4 import QtGui

from readers import loadFromJson


#I know that the password handling is painfully insecure, but firstly, this
#feature is for internal use by a bunch of doctors, and secondly anyway all
#the data is so open it barely matters. Still, I admit, this is so insecure
#as to not be there, but it is a required feature (albeit one told to me
#quite late) so what can I do?
def manage(preferences, mainWindow):
    """Handles anything that needs to be done with the preferences"""
    if preferences['password']!='None':
        users=loadFromJson(preferences['password'])
        users=dict((str(key), str(value)) for key,value in users.iteritems())
        userName=False
        while userName not in users:
            userName, _=QtGui.QInputDialog.getText(mainWindow, 'Login',
                                                   'User Name:')
            userName=str(userName)
        password=False
        while password!=users[userName]:
            password, _=QtGui.QInputDialog.getText(mainWindow, 'Login',
                                                   'Password:')
            password=str(password)
